import React, { useState,useEffect } from 'react';
import type { FormProps } from 'antd'
import {
  Button,
  Cascader,
  DatePicker,
  Form,
  Input,
  InputNumber,
  Radio,
  Select,
  Switch,
  TreeSelect,
} from 'antd';
import { useAppDispatch, useAppSelector } from '../../store/reactreduxts';
import * as actions from '../../store/crmsSlice'

type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};



const Myform: React.FC = (props) => {
    const [form]=Form.useForm();
 useEffect(()=>{
    form.setFieldsValue(props.itemobj)
 },[])
console.log(props.itemobj);
//获取状态
let obj=useAppSelector(state=>state.todos)
let dispatch=useAppDispatch()
  
//表单
const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
  if(props.title=='添加'){
    //添加对象
    let obj={
        ...values,
        id:new Date().setTime(),
        createTime:"2024-04-19 23:25:13"
    }
    //执行添加
    dispatch(actions.async_addshoplist(obj))
  }else{
    //编辑对象
    let obj={
        ...values,
        id:props.itemobj.id,
    }
    //执行编辑
    dispatch(actions.async_editshoplist(obj))
  }
  props.onCancel()
};


return (
    <Form onFinish={onFinish} form={form}>
      <Form.Item label="优惠卷名称" name="couponName"
         rules={[{ required: true, message: "不能为空!" }]}>
        <Input />
      </Form.Item>
      <Form.Item label="优惠卷面值" name="discountAmount"
         rules={[{ required: true, message: "不能为空!" }]}>
        <Input />
      </Form.Item>
      <Form.Item label="优惠卷最低消费" name="minSpend"
         rules={[{ required: true, message: "不能为空!" }]}>
        <Input />
      </Form.Item>
      <Form.Item label="优惠卷有效期" name="validityPeriod"
         rules={[{ required: true, message: "不能为空!" }]}>
        <Input />
      </Form.Item>
      <Form.Item label="排序" name="sortOrder">
        <InputNumber />
      </Form.Item>
      <Form.Item label="状态" name="status">
         <Radio.Group
              options={[
                { value: true, label: "是" },
                { value: false, label: "否" },
              ]}
            />
      </Form.Item>
        <Button type="primary" htmlType='submit' >完成</Button>
    </Form>
  );
};

export default Myform;